package com.peopledelete.APScheduler;

/**
 * @author cyt
 * @create 2020-06-15 14:04
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peopledelete.pojo.Dayrecords;
import com.peopledelete.pojo.Pre;
import com.peopledelete.pojo.Preorigin;
import com.peopledelete.pojo.X;
import com.peopledelete.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/****
 * 清洗 每日原始记录到 每日记录表中
 */
@Configuration()
@Slf4j
@Component
@EnableScheduling // 开启定时任务
public class GetDayRecords {

    @Autowired
    OrigindayrecordsService origindayrecordsService;

    @Autowired
    DayrecordsService dayrecordsService;

    @Autowired
    XService xService;

    @Autowired
    PreoriginService preoriginService;

    @Autowired
    PreService preService;


    /**
     * 清洗 每日原始记录到 每日记录表中
     * <p>
     * 22 点
     * http://cron.qqe2.com/
     * <p>
     * 定时任务：
     * 每日22 点开始统计出当日记录， 并且查出当日的外部因素， 一起存入数据库
     * <p>
     * dayrecords 中
     */

    // @Scheduled(cron = "0 0 0 ? ? ? *")
    public void initRecords() {
        // 得到 日期
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);

        // 存入 人数数据
        // dateString = "2020-06-12";
        int am_one = origindayrecordsService.initRecords(dateString, 0, 10);
        int am_two = origindayrecordsService.initRecords(dateString, 10, 14);
        int pm_one = origindayrecordsService.initRecords(dateString, 14, 16);
        int pm_two = origindayrecordsService.initRecords(dateString, 17, 22);
        int night = origindayrecordsService.initRecords(dateString, 22, 23);
        int total = am_one + am_two + pm_one + pm_two + night;
        Dayrecords dayrecords = new Dayrecords();
        dayrecords.setAmOne(am_one).setAmTwo(am_two).setPmOne(pm_one).setPmTwo(pm_two).setTotal(total).setDate(new Date()).setNight(night);
        // 存入条件因素
        X xInfo = xService.getXByDate(dateString);
        dayrecords.setWeather(xInfo.getWeather()).setAct(xInfo.getAct()).setTemp(xInfo.getTemp()).setFestival(xInfo.getFestival());

        dayrecordsService.save(dayrecords);
    }

    /**
     * 插入原始数据和预测数据
     */
    // @Scheduled(cron = "0 0 0 ? ? ? *")
    public void insertPreAndOrigin() {
        // 查出 预测人数
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);

        QueryWrapper<Pre> wrapper = new QueryWrapper<>();
        wrapper.eq("date", dateString);
        Pre pre = preService.getOne(wrapper);
        int preNum = pre.getTotal();
        // 查出 原始人数

        QueryWrapper<Dayrecords> Daywrapper = new QueryWrapper<>();
        Daywrapper.eq("date", dateString);

        Dayrecords dayrecords = dayrecordsService.getOne(Daywrapper);
        int origin = dayrecords.getTotal();

        //保存
        Preorigin preorigin = new Preorigin();
        preorigin.setOrigin(origin).setPre(preNum).setDate(new Date());

        preoriginService.save(preorigin);
    }
}

